package com.kexio.user.controller;

import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.kexio.common.dto.Result;
import com.kexio.user.entity.SysDictData;
import com.kexio.user.entity.SysDictType;
import com.kexio.user.service.DictService;

@RestController
@RequestMapping("/system/dict")
public class DictController {

    @Autowired
    private DictService dictService;

    @GetMapping("/types")
    public Result<List<SysDictType>> listTypes() {
        return Result.success("查询成功", dictService.listTypes());
    }

    @GetMapping("/types/{typeCode}/items")
    public Result<List<SysDictData>> listItems(@PathVariable("typeCode") String typeCode) {
        return Result.success("查询成功", dictService.listItems(typeCode));
    }

    @GetMapping("/batch")
    public Result<Map<String, List<SysDictData>>> listByTypes(@RequestParam("types") Collection<String> types) {
        return Result.success("查询成功", dictService.listByTypes(types));
    }

    @GetMapping("/label")
    public Result<String> getLabel(@RequestParam("type") String type,
                           @RequestParam("value") String value) {
        return Result.success("查询成功", dictService.getLabel(type, value));
    }

    @PostMapping("/types")
    public Result<Boolean> createType(@RequestBody SysDictType type) {
        return dictService.createType(type) ? Result.success("创建成功", true) : Result.error("创建失败");
    }

    @PutMapping("/types/{id}")
    public Result<Boolean> updateType(@PathVariable("id") String id, @RequestBody SysDictType type) {
        type.setId(id);
        return dictService.updateType(type) ? Result.success("更新成功", true) : Result.error("更新失败");
    }

    @DeleteMapping("/types/{id}")
    public Result<Boolean> deleteType(@PathVariable("id") String id) {
        return dictService.deleteType(id) ? Result.success("删除成功", true) : Result.error("删除失败");
    }

    @PostMapping("/items")
    public Result<Boolean> createItem(@RequestBody SysDictData item) {
        return dictService.createItem(item) ? Result.success("创建成功", true) : Result.error("创建失败");
    }

    @PutMapping("/items/{id}")
    public Result<Boolean> updateItem(@PathVariable("id") String id, @RequestBody SysDictData item) {
        item.setId(id);
        return dictService.updateItem(item) ? Result.success("更新成功", true) : Result.error("更新失败");
    }

    @DeleteMapping("/items/{id}")
    public Result<Boolean> deleteItem(@PathVariable("id") String id) {
        return dictService.deleteItem(id) ? Result.success("删除成功", true) : Result.error("删除失败");
    }
}


